<?php

namespace app\api\controller;

use app\common\business\Category as CategoryBis;

/**
 * 商城商品分类
 */
class Category extends ApiBase {

    /**
     * 获取所有分类的内容
     * @return \think\response\Json
     */
    public function index() {
        try {
            $categoryBusObj = new CategoryBis();
            $category = $categoryBusObj->getNormalAllCategorys();
        } catch (\Exception $e) {
            return show(config("status.success"), "内部异常");
        }
        if (!$category) {
            return show(config("status.success"), "数据为空");
        }

        $result = \app\common\lib\Arr::getTree($category);
        $result = \app\common\lib\Arr::sliceTreeArr($result);
        return show(config("status.success"), "OK", $result);
    }

    /**
     * api/category/search/51  10-7
     * 商品列表页面中 按栏目检索的内容
     * @return \think\response\Json
     */
    public function search() {
        $id = input('id', 0, 'intval');
        $result = (new CategoryBis())->search($id);
        return show(config("status.success"), "ok", $result);
    }

    /**
     * 获取三级分类  category/sub/2
     * @return \think\response\Json
     */
    public function sub() {
        $cid = input('param.id', 0, 'intval');
        if (!$cid) {
            return show(config("status.success"), "ok", []);
        }

        $result = (new CategoryBis())->getNormalByPids([$cid], 'id, name');
        return show(config("status.success"), "ok", $result);
    }
}